from celery import Celery
from flask_mail import Message
from exts import mail
from  flask import Flask
# from qfbbs import create_app
#在这里导入app
from utils.aliyunsdk.demo_sms_send import send_sms
import config

app = Flask(__name__)
app.config.from_object(config) #加載配置文件
mail.init_app(app)
#https://flask.palletsprojects.com/en/1.0.x/patterns/celery/
#攻略
def make_celery(app):
    celery = Celery(
        app.import_name,
        backend=app.config['CELERY_RESULT_BACKEND'],
        broker=app.config['CELERY_BROKER_URL']
    )
    celery.conf.update(app.config)

    class ContextTask(celery.Task):
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return self.run(*args, **kwargs)

    celery.Task = ContextTask
    return celery


celery = make_celery(app)
@celery.task
def send_mail(subject,recipients,body):
    message = Message(subject, recipients=recipients, body=body)
    mail.send(message)

@celery.task
def send_sms_capcha(telephone,captcha):
    send_sms(telephone,code=captcha)



